//src/utils.c
#include "utils.h"
#include <openssl/sha.h>
#include <xxhash.h> // 引入 xxhash.h
#include <stdarg.h>
#include <stdlib.h>

// calculate_sha256_hex 保持不变，用于OCI元数据
int calculate_sha256_hex(const unsigned char* data, size_t size, char* output) {
    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    if (!SHA256_Init(&sha256)) return -1;
    if (!SHA256_Update(&sha256, data, size)) return -1;
    if (!SHA256_Final(hash, &sha256)) return -1;
    for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
        sprintf(output + (i * 2), "%02x", hash[i]);
    }
    output[SHA256_HEX_LEN] = '\0';
    return 0;
}

// 新增 xxh64 计算函数，用于数据块
int calculate_xxh64_hex(const unsigned char* data, size_t size, char* output) {
    XXH64_hash_t const hash = XXH64(data, size, 0); // 0 is the seed
    // 将 64-bit 整数转换为 16 位的十六进制字符串
    snprintf(output, XXH64_HEX_LEN + 1, "%016llx", (unsigned long long)hash);
    return 0;
}

// run_command 保持不变
int run_command(const char* format, ...) {
    va_list args;
    char command[2048];
    va_start(args, format);
    vsnprintf(command, sizeof(command), format, args);
    va_end(args);
    
    LOG_INFO("执行命令: %s", command);
    int ret = system(command);
    if (ret != 0) {
        LOG_ERROR("命令执行失败, 返回码: %d", ret);
        return -1;
    }
    return 0;
}